/*
 * src/lexical/get_words2file.c
 * 
 * Harry Wei <harryxiyou@gmail.com> (C) 2012
 *
 * get each words and store into specific files.
 */
#include <stdio.h>
#include <glib.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include "common.h"
#include "cp_ctrl.h"

static int create_cfile(const char *file) {
	if (NULL == file) {
		g_message("%s -- parameter error", __func__);
		return -1;
	}
	int fd = 0;
	fd = open(file, O_CREAT | O_RDWR, 00777);
	if (-1 == fd) {
		g_message("%s -- create file error!", __func__);
		return -1;
	}
	return fd;
}

static inline int search_words(struct la *ctrl, int lec_fd, gchar *v) {
	g_message("enter func %s", __func__);
	g_message("9999 %p %s", ctrl, v);
	int i = 0;
	int ret = 0;
	char wbuf[TABLE_SIZE];
	memset(wbuf, 0, TABLE_SIZE);
	struct table *buf = ctrl->keyword_buff;
	g_message("key words num is %d", ctrl->kd_amount);
	for (i = 0; i < ctrl->kd_amount - 1; i++) { 
		/* XXX should write a func to avoid repeated works */
		printf("99991 %d %s\n", buf->code, buf->ch);
		if (0 == g_strcmp0(v, buf->ch)) {
			sprintf(wbuf, "%d %s\n", buf->code, buf->ch);
			ret = write(lec_fd, wbuf, strlen(wbuf));
			if (strlen(wbuf) != ret) {
				g_message("%s -- write error!", __func__);
				return -1;
			}
			goto out;
		}
		buf++;
	}
	buf = ctrl->operator_buff;
	g_message("key words num is %d", ctrl->op_amount);
	for (i = 0; i < ctrl->op_amount - 1; i++) {
		printf("99992 %d %s\n", buf->code, buf->ch);
		if (0 == g_strcmp0(v, buf->ch)) {
			sprintf(wbuf, "%d %s\n", buf->code, buf->ch);
			ret = write(lec_fd, wbuf, strlen(wbuf));
			if (strlen(wbuf) != ret) {
				g_message("%s -- write error!", __func__);
				return -1;
			}
			goto out;
		}
		buf++;
	}
	buf = ctrl->delimiter_buff;
	g_message("key words num is %d", ctrl->dt_amount);
	for (i = 0; i < ctrl->dt_amount - 1; i++) {
		printf("99993 %d %s\n", buf->code, buf->ch);
		if (0 == g_strcmp0(v, buf->ch)) {
			sprintf(wbuf, "%d %s\n", buf->code, buf->ch);
			ret = write(lec_fd, wbuf, strlen(wbuf));
			if (strlen(wbuf) != ret) {
				g_message("%s -- write error!", __func__);
				return -1;
			}
			goto out;
		}
		buf++;
	}
#if 1
	/* XXX easily handle constant and identifier conditions */
	printf("99994 %d %s\n", (int) 16, v);
	sprintf(wbuf, "%d %s\n", (int) 16, v);
	g_message("9999 --- %s", wbuf);
	ret = write(lec_fd, wbuf, strlen(wbuf));
	printf("99994 %d %s\n", (int) 16, v);
	if (strlen(wbuf) != ret) {
		g_message("%s -- write error!", __func__);
		return -1;
	}
#endif
out:
	g_message("leave func %s", __func__);
	return 0;
}

static int light_search(struct table *buf, int buf_len, int lec_fd, gchar *v) {
	g_message("enter func %s", __func__);
	g_message("%s -- 9999 %p %s %d", __func__, buf, v, buf_len);
	int i = 0;
	int ret = 0;
	char wbuf[TABLE_SIZE];
	memset(wbuf, '\0', TABLE_SIZE);
	g_message("key words num is %d", buf_len);
	g_message("lens: ch %d", sizeof(buf->ch));
	g_message("lens: ch %d", sizeof(v));
	for (i = 0; i < buf_len; i++) { 
		printf("99991 %d %s\n", buf->code, buf->ch);
		if (0 == g_strcmp0(v, buf->ch)) {
			sprintf(wbuf, "%d %s@@##$$", buf->code, buf->ch);
			ret = write(lec_fd, wbuf, TABLE_SIZE);
			if (TABLE_SIZE != ret) {
				g_message("%s -- write error!", __func__);
				return -1;
			}
			goto out;
		}
		buf++;
	}
out:
	return 0;
}

int get_words2file(struct la *ctrl) {
	g_message("enter func %s", __func__);
	if (NULL == ctrl || NULL == ctrl->buff) {
		g_message("%s -- Parameter error!", __func__);
		return -1;
	}
	int lec_fd = create_cfile(LEXICALFILE);
	if (-1 == lec_fd) {
		g_message("%s -- create_cfile error!", __func__);
		return -1;
	}
	int ret = 0;
	char buf[CHAR_SIZE];
	memset(buf, '\0', CHAR_SIZE);
	struct table tmp;
	memset(&tmp, '\0', sizeof(struct table));
	gchar **v = NULL;
	v = g_strsplit(ctrl->buff, " ", 0);
	int i = 0;
	int j = 0;
	for (i = 0; i < g_strv_length(v); i++) {
#if 0
		g_message("9999 %d %s", i, v[i]);
		sprintf(buf, "%s", v[i]);
		g_message("%s 999 -- %p, %u, %s", __func__, ctrl->keyword_buff, ctrl->kd_amount, buf);
		light_search(ctrl->keyword_buff, ctrl->kd_amount, lec_fd, buf);
#endif
#if 1
		if (0 == (ret = search_words(ctrl, lec_fd, v[i]))) {
			continue;
		} else if (-1 == ret) {
			g_message("%s -- provoke error!", __func__);
			goto out;
		}
#endif
	}
out:
	close(lec_fd);
	g_strfreev(v);
	g_message("leave func %s", __func__);
	return ret;
}
